﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace LicPdaService
{
    public class Login
    {
        public static string GetUser(string xml)
        {
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            try
            {
                //-------接收XML处理-------
                DataTable ldt1 = null;
                try
                {
                    ldt1 = App.Xml2DataTable(xml);
                    if (ldt1.Rows.Count == 0)
                    {
                        return "";
                    }
                }
                catch (Exception ex)
                {
                    App.Log("XML转DT失败", ex.Message.ToString());
                    throw;
                }

                try
                {
                    LicPdaService.App.Connection();
                }
                catch (Exception ex)
                {
                    App.Log("数据库连接失败", ex.Message.ToString());
                    throw;
                }
                DataTable ldt = null;
                try
                {
                    //strSql = "select whseid,userid, password,roleid from UserInfo where userid='" + ldt1.Rows[0]["username"].ToString() + "' and password='" + ldt1.Rows[0]["pwd"].ToString() + "'";
                    strSql = "select F.whseid,U.userid, U.password,r.roleid from UserInfo AS U ";
                    strSql += " LEFT JOIN UserRole AS R ON U.userId=R.userid ";
                    strSql += " LEFT JOIN RoleInfo AS F ON R.roleId=F.roleId";
                    strSql += " where U.userid='"+ ldt1.Rows[0]["username"].ToString() + "' and  U.password='"+ ldt1.Rows[0]["pwd"].ToString() + "'";
                    strSql += " order by F.whseid ";
                    ldt = App.GetDataTable(App.DBconWMS, strSql, "UserInfo");
                }
                catch (Exception ex)
                {
                    App.Log("LOGIN-GetTabel", ex.Message.ToString());
                    throw;
                }




                //-------返回XML处理-------
                XmlDocument doc = new XmlDocument();//返回报文
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlElement rootRet = doc.CreateElement("NewDataSet");
      
                if (ldt.Rows.Count > 0)
                {
                    for(int k = 0; k < ldt.Rows.Count; k++)
                    {
                        XmlNode userNodesRet = doc.CreateElement("login");//返回用户节点
                        XmlElement lxeWhseid = doc.CreateElement("whseid");
                        XmlElement lxeUserName = doc.CreateElement("username");
                        XmlElement lxePwd = doc.CreateElement("pwd");
                        XmlElement lxeCustomer_Code = doc.CreateElement("customer_code");
                        XmlElement lxeVendor = doc.CreateElement("vendor");

                        lxeWhseid.InnerText = ldt.Rows[k]["whseid"].ToString();
                        lxeUserName.InnerText = ldt.Rows[k]["userid"].ToString();
                        lxePwd.InnerText = ldt.Rows[k]["password"].ToString();
                        lxeCustomer_Code.InnerText = "";
                        lxeVendor.InnerText = "";
                        userNodesRet.AppendChild(lxeWhseid);
                        userNodesRet.AppendChild(lxeUserName);
                        userNodesRet.AppendChild(lxePwd);
                        userNodesRet.AppendChild(lxeCustomer_Code);
                        userNodesRet.AppendChild(lxeVendor);
                        rootRet.AppendChild(userNodesRet);
                    }
                }
                else
                {
                    XmlNode userNodesRet = doc.CreateElement("login");//返回用户节点
                    XmlElement lxeWhseid = doc.CreateElement("whseid");
                    XmlElement lxeUserName = doc.CreateElement("username");
                    XmlElement lxePwd = doc.CreateElement("pwd");
                    XmlElement lxeCustomer_Code = doc.CreateElement("customer_code");
                    XmlElement lxeVendor = doc.CreateElement("vendor");

                    lxeWhseid.InnerText = "";
                    lxeUserName.InnerText = "";
                    lxePwd.InnerText = "";
                    lxeCustomer_Code.InnerText = "";
                    lxeVendor.InnerText = "";
                    userNodesRet.AppendChild(lxeWhseid);
                    userNodesRet.AppendChild(lxeUserName);
                    userNodesRet.AppendChild(lxePwd);
                    userNodesRet.AppendChild(lxeCustomer_Code);
                    userNodesRet.AppendChild(lxeVendor);
                    rootRet.AppendChild(userNodesRet);
                }
                doc.AppendChild(rootRet);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
                App.Log("Login", ex.Message.ToString());
                throw;
            }

        }
    }
}
